package com.zhudi.develop_demo_as;


import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ZhudiActivity extends AppCompatActivity {
	private Context context;
	private InputMethodManager manager;
	//private ZhudiSwipeBackLayout layout;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		context = this;
	}
	
	/**
	 * 滑动关闭activity
	 * 子类activity需要android:theme="@android:style/Theme.Translucent"
	 * Theme.Translucent
	 * 布局文件
	 *<com.zhudi.develop.view.ZhudiSwipeBackLayout xmlns:android="http://schemas.android.com/apk/res/android"
     * xmlns:tools="http://schemas.android.com/tools"
     * android:layout_width="match_parent"
     * android:layout_height="match_parent" >
     * </com.zhudi.develop.view.ZhudiSwipeBackLayout>
	 */
//	public void swipeBack(){
//		layout = (ZhudiSwipeBackLayout) LayoutInflater.from(this).inflate(R.layout.swipeback, null);
//		layout.attachToActivity(this);
//	}

	
//	// Press the back button in mobile phone
//	@Override
//	public void onBackPressed() {
//		super.onBackPressed();
//		overridePendingTransition(0, R.anim.base_slide_right_out);
//	}

	/**
	 * 隐藏
	 * 
	 * @param view
	 */
	public void gone(View view) {
		view.setVisibility(View.GONE);
	}
	
	/**
	 * 隐藏
	 * @param id 控件Id
	 */
	public void gone(int id){
		findViewById(id).setVisibility(View.GONE);
	}

	/**
	 * INVISIBLE
	 * 
	 * @param view
	 */
	public void invisible(View view) {
		view.setVisibility(View.INVISIBLE);
	}
	
	/**
	 * INVISIBLE
	 * @param id 控件Id
	 */
	public void invisible(int id){
		findViewById(id).setVisibility(View.INVISIBLE);
	}
	
	/**
	 * 显示图片
	 * @param view
	 * @param resourceID
	 */
	public void setBackground(View view,int resourceID) {
		view.setBackgroundResource(resourceID);
	}
	
	/**
	 * 显示图片
	 * @param id
	 * @param resourceID
	 */
	public void setBackground(int id,int resourceID){
		findViewById(id).setBackgroundResource(resourceID);
	}
	
	/**
	 * 显示图片
	 * @param view
	 * @param resourceID
	 */
	public void setImageResource(View view,int resourceID) {
		((ImageView)view).setImageResource(resourceID);
	}
	
	/**
	 * 显示图片
	 * @param id
	 * @param resourceID
	 */
	public void setImageResource(int id,int resourceID){
		((ImageView)findViewById(id)).setImageResource(resourceID);
	}

	/**
	 * 显示
	 * 
	 * @param view
	 */
	public void visibility(View view) {
		view.setVisibility(View.VISIBLE);
	}
	
	/**
	 * 显示
	 * @param id 控件Id
	 */
	public void visibility(int id){
		findViewById(id).setVisibility(View.VISIBLE);
	}
	
	/**
	 * 设置背景颜色
	 * @param id 控件id
	 * @param colorId 颜色id
	 */
	public void setBackgroundColor(int id,int colorId){
		findViewById(id).setBackgroundColor(getResources().getColor(colorId));
	}
	
	/**
	 * 设置字体颜色
	 * @param id 控件id
	 * @param colorId 颜色id
	 */
	public void setTextColor(int id,int colorId){
		((TextView)findViewById(id)).setTextColor(getResources().getColor(colorId));
	}
	
	/**
	 * 获取TextView的内容
	 * 
	 * @param view
	 */
	public String getTextView(View view) {
		return ((TextView) view).getText().toString().trim();
	}
	
	/**
	 * 获取TextView的内容
	 * @param id 控件ID
	 * @return String
	 */
	public String getTextView(int id) {
		return ((TextView) findViewById(id)).getText().toString().trim();
	}

	/**
	 * 获取EditText的内容
	 * 
	 * @param view
	 */
	public String getEditText(View view) {
		return ((EditText) view).getText().toString().trim();
	}
	
	/**
	 * 获取EditText的内容
	 * @param id 控件ID
	 * @return String
	 */
	public String getEditText(int id) {
		return ((EditText) findViewById(id)).getText().toString().trim();
	}
	
	/**
	 * 设置edittext文字
	 * @param view 
	 * @param str 内容
	 */
	public void setEditText(View view, String str){
		visibility(view);
		((EditText) view).setText(str);
	}
	
	/**
	 * 设置edittext文字
	 * @param view 
	 * @param resourceID 内容Id
	 */
	public void setEditText(View view, int resourceID){
		visibility(view);
		((EditText) view).setText(getResources().getString(resourceID));
	}
	
	/**
	 * 设置edittext文字
	 * @param id 
	 * @param str 内容
	 */
	public void setEditText(int id, String str){
		visibility(id);
		EditText et = (EditText) findViewById(id);
		et.setText(str);
	}
	
	/**
	 * 设置edittext文字
	 * @param id 
	 * @param resourceID 内容
	 */
	public void setEditText(int id, int resourceID){
		visibility(id);
		EditText et = (EditText) findViewById(id);
		et.setText(getResources().getString(resourceID));
	}
	
	/**
	 * 设置TextView的标题
	 * 
	 * @param view 控件
	 * @param resourceID 资源ID
	 */
	public void setText(View view, int resourceID) {
		visibility(view);
		((TextView)view).setText(getResources().getString(resourceID));
	}
	
	
	/**
	 * 设置TextView的标题
	 * @param id 控件Id
	 * @param resourceID 资源ID
	 */
	public void setText(int id, int resourceID) {
		TextView tv = (TextView) findViewById(id);
		visibility(tv);
		tv.setText(getResources().getString(resourceID));
	}

	/**
	 * 设置TextView的标题
	 * 
	 * @param view 控件
	 * @param resource 字符串
	 */
	public void setText(View view, String resource) {
		visibility(view);
		((TextView)view).setText(resource);
	}
	
	/**
	 * 设置TextView的标题
	 * @param id 控件Id
	 * @param resource 字符串
	 */
	public void setText(int id, String resource) {
		TextView tv = (TextView) findViewById(id);
		visibility(tv);
		tv.setText(resource);
	}
	
	/**
	 * startActivity
	 * @param cls
	 */
	public void startIntentClass(Class<?> cls) {
		startActivity(new Intent(context, cls));
	}

	/**
	 * startActivity action
	 * @param action
	 * @param cls
	 */
	public void startIntentActionClass(String action, Class<?> cls) {
		Intent intent = new Intent();
		intent.setClass(context, cls);
		intent.setAction(action);
		startActivity(intent);
	}

	/**
	 * startActivity bundle
	 * @param bundle
	 * @param cls
	 */
	public void startIntentBundleClass(Bundle bundle, Class<?> cls) {
		Intent intent = new Intent();
		intent.setClass(context, cls);
		intent.putExtras(bundle);
		startActivity(intent);
	}

	/**
	 * startActivity action bundle
	 * @param action
	 * @param bundle
	 * @param cls
	 */
	public void startIntentActionBundleClass(String action, Bundle bundle,
			Class<?> cls) {
		Intent intent = new Intent();
		intent.setClass(context, cls);
		intent.putExtras(bundle);
		intent.setAction(action);
		startActivity(intent);
	}
	
	/**
	 * startIntentForResult
	 * @param cls
	 * @param requestCode
	 */
	public void startIntentForResult(Class<?> cls, int requestCode) {
		Intent intent = new Intent();
		intent.setClass(context, cls);
		startActivityForResult(intent, requestCode);
	}
	
	/**
	 * startActivityForResult action
	 * @param action
	 * @param cls
	 * @param requestCode
	 */
	public void startIntentActionForResult(String action, Class<?> cls,
			int requestCode) {
		Intent intent = new Intent();
		intent.setClass(context, cls);
		intent.setAction(action);
		startActivityForResult(intent, requestCode);
	}
	
	/**
	 * startActivityForResult bundle
	 * @param bundle
	 * @param cls
	 * @param requestCode
	 */
	public void startIntentBundleForResult(Bundle bundle, Class<?> cls,
			int requestCode) {
		Intent intent = new Intent();
		intent.setClass(context, cls);
		intent.putExtras(bundle);
		startActivityForResult(intent, requestCode);
	}
	
	/**
	 * startActivityForResult action bundle
	 * @param action 
	 * @param bundle  
	 * @param cls
	 * @param requestCode
	 */
	public void startIntentActionBundleForResult(String action,
			Bundle bundle, Class<?> cls, int requestCode) {
		Intent intent = new Intent();
		intent.setClass(context, cls);
		intent.putExtras(bundle);
		intent.setAction(action);
		startActivityForResult(intent, requestCode);
	}
	
	/**
	 * 
	 * setIntentResult
	 * @param resultCode 
	 */
	public void setIntentResult(int resultCode){
		setResult(resultCode);
		finish();
	}
	
	/**
	 * 
	 * setIntentActivityBundleResult
	 * @param bundle 
	 * @param cls 
	 * @param resultCode 
	 */
	public void setIntentResult(Bundle bundle,Class<?> cls,int resultCode){
		Intent intent = new Intent(context, cls);
		intent.putExtras(bundle);
		setResult(resultCode, intent);
		finish();
	}
	
	
	/**
	 * 监听页面返回按钮
	 * 
	 * @param view 控件
	 */
	public void backActivity(View view) {
		view.setVisibility(View.VISIBLE);
		view.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	/**
	 * 返回APP应用首页activity
	 * @param view 控件
	 * @param cls 首页activity
	 */
	public void backHomeActivity(View view, final Class<?> cls) {
		visibility(view);
		view.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context,cls);
				intent.addCategory(Intent.CATEGORY_HOME);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
				startActivity(intent);
			}
		});
	}
	
	/**
	 * 拨打电话
	 * @param phoneNumber 拨打的号码
	 */
	public void goCallPhone(String phoneNumber) {
		Uri telUri = Uri.parse("tel:" + phoneNumber);
		Intent intent= new Intent(Intent.ACTION_DIAL, telUri);
		startActivity(intent);
	}
	
	/**
	 * 发送短信
	 * @param phoneNumber 接收者手机号
	 * @param content  发送的内容
	 */
	public void goSendSms(String phoneNumber,String content) {
		Uri smsToUri = Uri.parse("smsto:" + phoneNumber);
		Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
		intent.putExtra("sms_body", content);
		context.startActivity(intent);
	}
	
	/**
	 * 跳转浏览器
	 * @param url 网页的地址
	 */
	public void goWeb(String url) {
		Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
		context.startActivity(intent);
	}

	
	/**
	 * 点击屏幕关闭键盘
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
				manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			}
		}
		return super.onTouchEvent(event);
	}
	
	
	/**
	 * 解决因listview外层有scrollview造成的高度问题 listview内部的最外层布局框架必须为LinearLayout,
	 * 相对布局，在2.2,2.3可能会出问题
	 * 
	 * @param listView
	 */
	public static void resetListViewHeight(ListView listView) {
		// 获取ListView对应的Adapter
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			return;
		}

		int totalHeight = 0;
		for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0); // 计算子项View 的宽高
			totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
		}

		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		listView.setLayoutParams(params);
	}
	
	
	public static final long INTERVAL = 500; // 防止连续点击的时间间隔
	private static long lastClickTime = 0L; // 上一次点击的时间

	/**
	 * 防止按钮在规定时间内被连续点击的简单方法
	 * @param longTime 毫秒 默认为0.5s 
	 * @return Boolean true or false
	 */
	public static boolean filterClick(String longTime) {
		final long SELECT_INTERVAL;
		if (longTime != null) {
			SELECT_INTERVAL = Long.parseLong(longTime);
		} else {
			SELECT_INTERVAL = INTERVAL;
		}
		long time = System.currentTimeMillis();
		if ((time - lastClickTime) < SELECT_INTERVAL) {
			return false;
		}
		lastClickTime = time;
		return true;
	}
}
